
package sample.multithreading;

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import sample.multithreading.PicasaContentDB.PicasaFeatured;
import compatible.actionbar.ActionBarFragmentActivity;
import compatible.actionbar.CompatibleActionBar;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class DownloaderActivity extends ActionBarFragmentActivity
        implements LoaderManager.LoaderCallbacks<Cursor>
{
    private static final int NUM_LINES = 100;
    CursorAdapter mAdapter;
    String[] mListItems = new String[NUM_LINES];
    ListView mListView;
    // 	  For step: Communicating back from the service
    //    ResponseReceiver mReceiver;
    Intent mServiceIntent;
    int mTotalLines;
    TextView mActionBarProgressText;
    private static final String PICASA_RSS_URL = "http://picasaweb.google.com/data/feed/base/featured?alt=rss&kind=photo&access=public&slabel=featured&hl=en_US&imgmax=1600";
    
    private void setProgressText(int paramInt) {
        if (this.mActionBarProgressText != null)
            this.mActionBarProgressText.setText(paramInt);
    }
    
    public void showProgress(boolean paramBoolean) {
        getCompatibleActionBar().showCustomView(paramBoolean);
    }
    
    public ListView getListView()
    {
        if (this.mListView == null) {
            this.mListView = ((ListView) findViewById(android.R.id.list));
        }
        return this.mListView;
    }

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        CompatibleActionBar localCompatibleActionBar = getCompatibleActionBar();
        
        setContentView(android.R.layout.list_content);
        
        localCompatibleActionBar.setBackgroundDrawable(getResources()
                .getDrawable(R.drawable.actionbarbg));
        localCompatibleActionBar.setDisplayUseLogoEnabled(true);
        localCompatibleActionBar.setLogo(R.drawable.icon);
        View localView = getLayoutInflater().inflate(R.layout.progress, null);
        localCompatibleActionBar.setCustomView(localView);
        this.mActionBarProgressText = ((TextView) localView
                .findViewById(R.id.actionBarProgressText));
        IntentFilter filter = new IntentFilter(NetworkDownloadService.BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        
// 	  For step: Communicating back from the service
//        this.mReceiver = new ResponseReceiver();
//        LocalBroadcastManager.getInstance(this).registerReceiver(this.mReceiver, filter);


        this.mAdapter = new PicasaListAdapter(this);
        getListView().setAdapter(this.mAdapter);
        getSupportLoaderManager().initLoader(0, null, this);
        this.mServiceIntent = new Intent(this, NetworkDownloadService.class);
        Intent localIntent = this.mServiceIntent;

        Uri localUri = Uri
                .parse(PICASA_RSS_URL);

        localIntent.setData(localUri);

        startService(this.mServiceIntent);
    }
    
 //   private static final int CURSOR_ID_INDEX = 0;
    private static final int IMAGE_THUMB_URL_INDEX = 2;
//    private static final int IMAGE_URL_INDEX = 1;    

    private static final String[] PROJECTION;
    static
    {
        String[] arrayOfString = new String[3];
        arrayOfString[0] = PicasaFeatured._ID;
        arrayOfString[1] = PicasaFeatured.IMAGE_URL;
        arrayOfString[2] = PicasaFeatured.IMAGE_THUMB_URL;

        PROJECTION = arrayOfString;
    }
    
    public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle)
    {
        CursorLoader cl = new CursorLoader(this);
        cl.setUri(PicasaContentDB.getUriByType(this, PicasaContentDB.METADATA_QUERY));
        cl.setProjection(PROJECTION);
        return cl;
    }

    public void onDestroy()
    {
// 	  For step: Communicating back from the service
//        if (this.mReceiver != null) {
//            LocalBroadcastManager.getInstance(this).unregisterReceiver(this.mReceiver);
//            this.mReceiver = null;
//        }
        this.mListView = null;
        this.mAdapter = null;
        super.onDestroy();
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
    {
        this.mAdapter.changeCursor(cursor);
    }

    public void onLoaderReset(Loader<Cursor> loader)
    {
        if ( null != mAdapter ) {
            this.mAdapter.changeCursor(null);
        }
    }

    protected void onStop()
    {
        super.onStop();
    }

    private class PicasaListAdapter extends CursorAdapter
    {        
        public PicasaListAdapter(Activity act)
        {
            super(act, null, false);
        }

        public void bindView(View view, Context cxt, Cursor cursor)
        {
            TextView tv = (TextView) view;
            String str = cursor.getString(IMAGE_THUMB_URL_INDEX);
            tv.setText(str);
        }

        public View newView(Context cxt, Cursor cursor, ViewGroup parent)
        {
            LayoutInflater li = LayoutInflater.from(cxt);
            View v = li.inflate(R.layout.logitem, parent, false);
            return v;
        }

    }

// 	  For step: Communicating back from the service
//    private class ResponseReceiver extends BroadcastReceiver
//    {
//        private ResponseReceiver() {}
//
//        @Override
//        public void onReceive(Context paramContext, Intent paramIntent) {
//            switch (paramIntent.getIntExtra(NetworkDownloadService.EXTRA_STATUS,
//                    NetworkDownloadService.STATE_ACTION_COMPLETE)) {
//                case NetworkDownloadService.STATE_ACTION_STARTED:
//                    setProgressText(R.string.progress_starting_update);
//                    showProgress(true);
//                    break;
//                case NetworkDownloadService.STATE_ACTION_CONNECTING:
//                    setProgressText(R.string.progress_connecting);
//                    showProgress(true);
//                    break;
//                case NetworkDownloadService.STATE_ACTION_PARSING:
//                    setProgressText(R.string.progress_parsing);
//                    showProgress(true);
//                    break;
//                case NetworkDownloadService.STATE_ACTION_WRITING:
//                    setProgressText(R.string.progress_writing);
//                    showProgress(true);
//                    break;
//                case NetworkDownloadService.STATE_ACTION_COMPLETE:
//                    showProgress(false);
//                    break;
//            }
//        }
//    }
}
